using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


public class TestDialogAndFileObject : MonoBehaviour
{
    /*
    1. 配置文件测试: 自动获取系统语言
    2. 翻译模块测试
    3. 对话模块测试 
    */
    private void Awake()
    {
    }
    // Start is called before the first frame update
    void Start()
    {
        // DialogManager.instance.load_csv();
        // ObjectPoolManager.instance.load_csv()

        var statistic_file_obj = new AppFileTools.FileObject(file_name: "statistic.json", dir_name:"archives", create_if_not_exists: true);
        
        // int run_times = statistic_file_obj.content_dc.ContainsKey("run_times")? BdUtils.conv_to_int(statistic_file_obj.content_dc["run_times"], 0) : 0;
        int run_times = statistic_file_obj.content_dc.TryGetValue("run_times", out var tmp) ? BdUtils.conv_to_int(tmp) : 0;

        run_times += 1;
        statistic_file_obj.content_dc["run_times"] = run_times;
        // statistic_file_obj.open_dir(is_persistent_dir: true);
        
        BdUtils.log($"=== statistic_file_obj [{statistic_file_obj.relative_file_path}] --- run_times:", run_times);

        statistic_file_obj.save();
        
        // var test_file_obj = new AppFileUtils.FileObject(file_name: "use_info.json", dir_name: "archives", overwrite: true, merge_old_dc: true);
        var test_file_obj =
            new AppFileTools.FileObject(file_name: "use_info.json", dir_name: "archives", merge_old_dc: true);

        if (BdTools.Flags.is_unity_editor)
            test_file_obj.remove_existed_persistent_file(is_reload_content: true);
        
        // var test_file_obj = new AppFileUtils.FileObject(file_name: "test.txt");

        // BdUtils.log("=== content_text:", test_file_obj.content_text);
        // BdUtils.log("=== is_json_content:", test_file_obj.is_json_content);
        // BdUtils.log("=== conv_json_to_dict:", BdUtils.conv_json_to_dict(test_file_obj.content_text));

        // BdUtils.log("=== test_file_obj.streaming_file_path:", test_file_obj.streaming_file_path);
        // BdUtils.log("=== test_file_obj.persistent_file_path:", test_file_obj.persistent_file_path);
        
        BdUtils.log("=== test_file_obj.content_dc:", test_file_obj.content_dc);

        // test_file_obj.open_dir(is_persistent_dir: true);
        // test_file_obj.open_dir(is_persistent_dir: false);
        // test_file_obj.content_dc["age"] = BdUtils.conv_to_int(test_file_obj.content_dc["age"]) + 1;

        // if (test_file_obj)

        // var old_language = test_file_obj.content_dc["language"];
        // var old_is_cn = BdUtils.conv_to_bool(
        //     BdUtils.py_get_dc_value(test_file_obj.content_dc, "is_cn", true));

        // test_file_obj.content_dc.TryGetValue("is_cn", out old_is_cn);

        // --- 这里是根据系统语言来自动设置, 也可以用button的回调函数来让用户自己选择语言
        bool is_cn;
        BdUtils.log("=== on ContainsKey[`is_cn`]:", test_file_obj.content_dc.ContainsKey("is_cn"));
        
        if (test_file_obj.content_dc.ContainsKey("is_cn"))
        {
            is_cn = BdUtils.conv_to_bool(test_file_obj.content_dc["is_cn"]);
        }
        else
        {
            BdUtils.log("*** on auto get language!");
            test_file_obj.content_dc["language"] =
                BdUtils.conv_to_string(AppLanguageTools.LanguageTools.get_language());
            is_cn = AppLanguageTools.LanguageTools.is_cn();
            test_file_obj.content_dc["is_cn"] = is_cn;
            test_file_obj.save();
        }

        BdUtils.log("~~~ language:", test_file_obj.content_dc["language"]);
        BdUtils.log("~~~ is_cn:", test_file_obj.content_dc["is_cn"]);

        DialogManager.instance.AddEventListener("play_animation_0", play_animation_0);
        DialogManager.instance.AddEventListener("play_animation_1", play_animation_1);
        DialogManager.instance.AddEventListener("play_animation_2", play_animation_2);
        
        TranslatorManager.instance.is_cn = BdUtils.conv_to_bool(test_file_obj.content_dc["is_cn"]);
        TranslatorManager.instance.template_variable_dc.Add("name", test_file_obj.content_dc["name"]);
        TranslatorManager.instance.template_variable_dc.Add("age", test_file_obj.content_dc["age"]);
        TranslatorManager.instance.template_variable_dc.Add("sex", test_file_obj.content_dc["sex"]);
        // TranslatorManager.instance.template_variable_dc.Add("level", test_file_obj.content_dc["t"]);
    }

    void play_animation_0(string msg)
    {
        BdUtils.log("====== on play_animation_0! msg:", msg);
    }
    
    void play_animation_1(string msg)
    {
        BdUtils.log("====== on play_animation_1! msg:", msg);
    }

    void play_animation_2(string msg)
    {
        BdUtils.log("====== on play_animation_2! msg:", msg);
    }

    void test_1()
    {
        // var parent_trans = BdTools.get_game_object_by_path("/DialogCanvas", raise_error: true).transform;
        var parent_trans = BdTools.get_game_object_by_path("/DialogCanvas").transform;
        BdUtils.log("=== parent_trans:", parent_trans.name, parent_trans.position);
        BdUtils.assert(BdTools.get_game_object_by_path("/DialogCanvas"), "未找到`DialogCanvas`?");
        
        var box_position = BdTools.get_position_by_screen_scale(0.2f, 0.3f);

        var message_box_obj = BdTools.show_message_box("标题", "内容123123", parent_trans: parent_trans, box_type: 1,
            position: box_position);
        var message_btn_comp = message_box_obj.transform.Find("btn_confirm").GetComponent<Button>();
        message_btn_comp.transform.GetComponentInChildren<Text>().text = "返回";

        // --- 这里不能用回调函数`message_btn_comp.onClick.AddListener(OnClickMessageBoxBtn);`, 而是直接调用代码. 因为instance被销毁, 回调函数无效!
        message_btn_comp.onClick.AddListener(() => { BdUtils.log(" *** OnClickMessageBoxBtn!"); });
    }

    // Update is called once per frame
    void Update()
    {
    }
}